import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class NoneStyleInputField extends StatefulWidget {
  final String hintText;
  final ValueChanged<String>? onTextChanged;
  final TextStyle? textStyle;
  final TextStyle? hintTextStyle;
  final Color? bgColor;
  final EdgeInsetsGeometry? contentPadding;
  final TextInputType? keyboardType; // 添加键盘类型参数
  final List<TextInputFormatter>? inputFormatters; // 添加输入格式化器参数

  const NoneStyleInputField({
    super.key,
    required this.hintText,
    this.textStyle,
    this.hintTextStyle,
    this.onTextChanged,
    this.bgColor,
    this.contentPadding,
    this.keyboardType,
    this.inputFormatters,
  });

  @override
  NoneStyleInputFieldState createState() => NoneStyleInputFieldState();
}

class NoneStyleInputFieldState extends State<NoneStyleInputField> {
  final TextEditingController _controller = TextEditingController();

  @override
  void initState() {
    super.initState();
    _controller.addListener(_onTextChanged);
  }

  @override
  void dispose() {
    _controller.removeListener(_onTextChanged);
    _controller.dispose();
    super.dispose();
  }

  void _onTextChanged() {
    if (widget.onTextChanged != null) {
      widget.onTextChanged!(_controller.text);
    }
  }

  @override
  Widget build(BuildContext context) {
    return TextField(
      controller: _controller,
      style:
          widget.textStyle ??
          TextStyle(
            color: Colors.white,
            fontSize: 15.0,
            fontWeight: FontWeight.w400,
          ),
      keyboardType: widget.keyboardType, // 设置键盘类型
      inputFormatters: widget.inputFormatters, // 设置输入格式化器
      // 核心样式配置
      decoration: InputDecoration(
        // 1. 设置提示文本
        hintText: widget.hintText,
        // 2. 开启背景填充
        filled: true,
        // 3. 设置浅灰色填充颜色
        fillColor: widget.bgColor ?? Colors.transparent,
        // 4. 内容边距（可选，用于微调文本位置）
        contentPadding:
            widget.contentPadding ??
            const EdgeInsets.symmetric(horizontal: 16.0, vertical: 10.0),

        // 5. 移除默认的底部下划线/边框
        border: OutlineInputBorder(
          borderSide: BorderSide.none, // 移除边框线
        ),

        // 6. 移除获得焦点时的边框线
        focusedBorder: OutlineInputBorder(borderSide: BorderSide.none),

        // 7. 移除非激活状态时的边框线
        enabledBorder: OutlineInputBorder(borderSide: BorderSide.none),

        // 8. 提示文字的样式
        hintStyle:
            widget.hintTextStyle ??
            TextStyle(
              color: Colors.white60,
              fontSize: 15.0,
              fontWeight: FontWeight.w400,
            ),
      ),
    );
  }
}